#ifndef TOOLS_H
#define TOOLS_H
#include <glad/glad.h> // 顺序不能换
#include <GLFW/glfw3.h>
#include <stb_image.h>

#include <iostream>

unsigned char *imgData;
// int TEXCOORDUSED = 0;

class Texture{
public:
    // int TexCoodId;
    short textureType;
    unsigned int id;
    int width, height, channels;
    Texture(const char *imgPath, short sourceImgType = GL_RGBA, short targetImgType = GL_RGB, bool verticalFilp = true, short textureType = GL_TEXTURE_2D, short wrap = GL_REPEAT, short textureFilter = GL_LINEAR, bool genMipmap = true) : textureType(textureType)
    {
        glGenTextures(1, &id);
        glBindTexture(textureType, id);

        // 开始对此纹理的设定

        // 环绕方式
        glTexParameteri(textureType, GL_TEXTURE_WRAP_S, wrap);
        glTexParameteri(textureType, GL_TEXTURE_WRAP_T, wrap);
        // 纹理采样
        glTexParameteri(textureType, GL_TEXTURE_MIN_FILTER, textureFilter);
        glTexParameteri(textureType, GL_TEXTURE_MAG_FILTER, textureFilter);
        stbi_set_flip_vertically_on_load(verticalFilp);
        imgData = stbi_load(imgPath, &width, &height, &channels, 0);
        if (imgData)
        {
            glTexImage2D(textureType, 0, targetImgType, width, height, 0, sourceImgType, GL_UNSIGNED_BYTE, imgData);
            if(genMipmap){
                glGenerateMipmap(GL_TEXTURE_2D);
            }
        }
        else
        {
            std::cout << "Failed To Load Texture" << std::endl;
        }
        stbi_image_free(imgData);
        // TexCoodId = TEXCOORDUSED++;
    }

    static void BindTexCood(int TexCoordId, Texture texture){
        glActiveTexture(GL_TEXTURE0 + TexCoordId);
        glBindTexture(texture.textureType, texture.id);
    }
};

#endif